﻿using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Radzen;
using Swimj.RazorLib.Models;

namespace Swimj.RazorLib.Partials
{
    public partial class SearchTablePartial<T> : ComponentBase
    {
        bool isLoading;
        bool allowRowSelectOnRowClick = true;
        string pagingSummaryFormat = "Displaying page {0} of {1} <b>(total {2} records)</b>";
        IEnumerable<int> pageSizeOptions = new int[] { 10, 20, 30 };

        [Inject]
        [NotNull]
        public NotificationService? NotificationService { get; set; }

        [Parameter]
        public SearchPartialInfo SearchPartial { get; set; } = new();

        [Parameter] public Func<MouseEventArgs, Task>? AddHandler { get; set; }

        [Parameter]
        public EventCallback<T> DetailHandler { get; set; }

        [Parameter] public Func<MouseEventArgs, T, Task>? DeleteHandler { get; set; }

        [Parameter] public Func<MouseEventArgs, T, Task>? EditHandler { get; set; }

        [Parameter] public Func<MouseEventArgs, Task>? ImportExcelHandler { get; set; }

        [Parameter] public EventCallback ImportHistoryHandler { get; set; }

        [Parameter] public EventCallback ExportExcelHandler { get; set; }

        [Parameter] public EventCallback ExportHistoryHandler { get; set; }

        [Parameter] public RenderFragment? ChildContent { get; set; }

        [Parameter] public RenderFragment? ToolbarContent { get; set; }

        [Parameter] public RenderFragment<T>? TemplateColumnContent { get; set; }

        protected List<TablePartialColumn<T>> Columns { get; } = new();

        [Parameter]
        public int Count { get; set; }

        [Parameter]
        public IEnumerable<T>? Data { get; set; }

        [Parameter] public Func<LoadDataArgs, Task<ODataServiceResult<T>>>? ServerData { get; set; }

        public void AddColumn(TablePartialColumn<T> column)
        {
            Columns.Add(column);
        }

        async Task LoadData(LoadDataArgs args)
        {
            isLoading = true;

            if (ServerData != null)
            {
                var result = await ServerData.Invoke(args);
                // Update the Data property
                Data = result.Value.AsODataEnumerable();
                // Update the count
                Count = result.Count;
            }

            isLoading = false;
        }

        protected async Task GridDeleteButtonClick(MouseEventArgs args, T context)
        {
            try
            {
                if (DeleteHandler != null)
                {
                    await DeleteHandler.Invoke(args, context);
                }
            }
            catch (Exception ex)
            {
                NotificationService.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Error,
                    Summary = $"Error",
                    Detail = $"Unable to delete row"
                });
            }
        }

        protected async Task GridEditButtonClick(MouseEventArgs args, T context)
        {
            try
            {
                if (EditHandler != null)
                {
                    await EditHandler.Invoke(args, context);
                }
            }
            catch (Exception ex)
            {
                NotificationService.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Error,
                    Summary = $"Error",
                    Detail = $"Unable to edit row"
                });
            }
        }

        protected async Task GridAddButtonClick(MouseEventArgs args)
        {
            try
            {
                if (AddHandler != null)
                {
                    await AddHandler.Invoke(args);
                }
            }
            catch (Exception ex)
            {
                NotificationService.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Error,
                    Summary = $"Error",
                    Detail = $"Unable to edit row"
                });
            }
        }

        protected async Task GridImportExcelButtonClick(MouseEventArgs args)
        {
            try
            {
                if (ImportExcelHandler != null)
                {
                    await ImportExcelHandler.Invoke(args);
                }
            }
            catch (Exception ex)
            {
                NotificationService.Notify(new NotificationMessage
                {
                    Severity = NotificationSeverity.Error,
                    Summary = $"Error",
                    Detail = $"Unable to edit row"
                });
            }
        }

        public async Task ReloadServerData()
        {
            await _requestTable.Reload();
        }
    }
}